<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>地址管理</title>
    <link rel="stylesheet" href="css/base.css" />
    <link rel="stylesheet" href="css/all.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/address-administration.css" />
</head>
<body>
<div>
    <div class="head">
         <span onclick="go()">
        <i class="iconfont icon-iconfontjiantou1"></i></span>
        <h1 class="titles">地址管理</h1>
    </div>
    <div class="middles" id="myAllAddressList">
    </div>
    <br/><br/><br/><br/><br/>
    <div class="footer">
        <div onclick="location.href='./add-address.html'" class="add">
            <span>添加新地址</span>
        </div>
    </div>
</div>
</body>
<script src="js/x_rem.js"></script>
<script src="js/all.js"></script>
<script src="./js/jquery-1.10.2.js"></script>

<script>
    $(function() {
        var data = JSON.stringify({

        });
        var success = (data) => {
            console.log(data.message.code);
            if(data.message.code == 200) {
                var html = "";
                if(data.data != null&& data.data.addressList != ''){
                    for(let i = 0; i<data.data.addressList.length; i++) {
                        var str =``;
                        var addressId= data.data.addressList[i].id;
                        if(data.data.addressList[i].isdefault == 0){
                            str = `<i class="iconfont icon-selected-copy" onclick="defaultAddress(` + addressId + `)"></i><span>设为默认地址</span>`;
                        }
                        var addressId = data.data.addressList[i].id;
                        html += `
                            <div>
                                  <div onclick="location.href='./buy-body-address.html?addressId=` + addressId + `'" class="consignee">
                                      <span>收货人：${data.data.addressList[i].name}</span><span>${data.data.addressList[i].mobile}</span>
                                      <p>取货地址：${data.data.addressList[i].pcadetail}, ${data.data.addressList[i].address}</p>
                                  </div>
                                  <div class="editor">
                                  `+str+`
                                      <div class="delete" onclick="deleteAddress(` + addressId + `)">
                                          <span>删除</span>
                                      </div>
                                      <div onclick="location.href='./edit-address.html?addressId=` + addressId + `'" class="edit">
                                          <span>编辑</span>
                                      </div>
                                  </div>
                             </div>`
                    }
                    $("#myAllAddressList").html(html);
                }
            } else {
                popup(data.message.msg);
            }
        };
        Hajax("post", "/app/account/address", data, success);
    });
    
    function deleteAddress(addressId) {
        $.ajax({
            type: "POST",
            url: "app/account/delAddress",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "addressId": addressId
            }),
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
                XMLHttpRequest.setRequestHeader("userId", userId);
            },
            error: function (request) {
                alert("Connection error ");
            },
            success: function (data) {
                //layer.msg(data.message.msg);
                location.reload();
            }
        });
    }

    function defaultAddress(addressId) {
        $.ajax({
            type: "POST",
            url: "app/account/defaultAddress",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "addressId": addressId
            }),
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
                XMLHttpRequest.setRequestHeader("userId", userId);
            },
            error: function (request) {
                alert("Connection error ");
            },
            success: function (data) {
                //layer.msg(data.message.msg);
                location.reload();
            }
        });
    }
</script>
</html>